package com.goldcn.picture.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.goldcn.common.CommonTools;
import com.goldcn.common.exception.ServiceException;
import com.goldcn.common.util.CommonUtils;
import com.goldcn.common.util.IdGen;
import com.goldcn.enumeration.ErrorCodeEnum;
import com.goldcn.goods.dto.PageInfo;
import com.goldcn.picture.dao.PicSpecialDetailMapper;
import com.goldcn.picture.dao.PicSpecialMapper;
import com.goldcn.picture.dto.QueryDetailDto;
import com.goldcn.picture.model.PicSpecial;
import com.goldcn.picture.model.PicSpecialDetail;
import com.goldcn.picture.vo.PicSpecialDetailVo;
import com.goldcn.picture.vo.PicSpecialVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
@Slf4j
public class PicSpecialService {

    @Autowired
    PicSpecialMapper picSpecialMapper ;

    @Autowired
    PicSpecialDetailMapper picSpecialDetailMapper;


    /**
     * 添加专题
     * @param picSpecialVo
     * @return
     */
    public Integer addPicSpecial(PicSpecialVo picSpecialVo){
        try {
            PicSpecial picSpecial = new PicSpecial();
            picSpecial.setSpecialid(IdGen.get().nextId());
            picSpecial.setAddtime(CommonUtils.getCurTimestamp());
            picSpecial.setSpecialdes(picSpecialVo.getSpecialdes());
            picSpecial.setSpecialname(picSpecialVo.getSpecialname());
            picSpecial.setSeq(picSpecialVo.getSeq());
            picSpecial.setUserid(picSpecialVo.getUserid());
            picSpecial.setUsername(picSpecialVo.getUsername());

            Integer result = picSpecialMapper.insertSelective(picSpecial);
            if (result != 1) {
                throw CommonTools.createException(ErrorCodeEnum.addSpecialFailure);
            }
            if (picSpecialVo.getPicturte().size() > 0) {
                for (String pic : picSpecialVo.getPicturte()) {
                    PicSpecialDetail detail = new PicSpecialDetail();
                    detail.setAddtime(CommonUtils.getCurTimestamp());
                    detail.setSpecialdetailid(IdGen.get().nextId());
                    detail.setPictureid(Long.parseLong(pic));
                    detail.setSpecialid(picSpecial.getSpecialid());
                    detail.setSpecialname(picSpecial.getSpecialname());
                    picSpecialDetailMapper.insertSelective(detail);
                }

            }
            return result;
        }catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.info(e.getMessage());
            throw CommonTools.createException(ErrorCodeEnum.operateOtherExceptionFailure);
        }
    }


    /**
     * 更新专题
     * @param picSpecialVo
     * @return
     */
    public Integer updateByPicSpecial(PicSpecialVo picSpecialVo){

        try {
            PicSpecial picSpecial = new PicSpecial();
            BeanUtils.copyProperties(picSpecialVo, picSpecial);

            Integer result = picSpecialMapper.updateByPrimaryKeySelective(picSpecial);
            if (result != 1) {
                throw CommonTools.createException(ErrorCodeEnum.updateSpecialFailure);
            }
/*            picSpecialDetailMapper.deleteBySpecialId(picSpecialVo.getSpecialid());

            if (picSpecialVo.getPicturte().size() > 0) {
                for (String pic : picSpecialVo.getPicturte()) {
                    PicSpecialDetail detail = new PicSpecialDetail();
                    detail.setAddtime(CommonUtils.getCurTimestamp());
                    detail.setSpecialdetailid(IdGen.get().nextId());
                    detail.setPictureid(Long.parseLong(pic));
                    detail.setSpecialid(picSpecialVo.getSpecialid());
                    detail.setSpecialname(picSpecialVo.getSpecialname());
                    picSpecialDetailMapper.insertSelective(detail);
                }
            }*/
            return result;
        }catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.info(e.getMessage());
            throw CommonTools.createException(ErrorCodeEnum.operateOtherExceptionFailure);
        }
    }


    /**
     * 通过ID删除指定的专题
     * @param specialID
     * @return
     */
    public Integer deleteByPrimaryKey(Long specialID){
        //判断标签是否使用
/*        if(picLabelMapper.labelUseCount(labelID)>0){
            throw CommonTools.createException(ErrorCodeEnum.lableIsUsed);
        }*/
        Integer result = picSpecialMapper.deleteByPrimaryKey(specialID);
        if(result!=1){
            //抛出异常
            throw CommonTools.createException(ErrorCodeEnum.deleteSpecialFailure);
        }
        picSpecialDetailMapper.deleteBySpecialId(specialID);
        return result;
    }

    /**
     * 根据ID查询专题
     * @param specialID
     * @return
     */
    public PicSpecial selectByPrimaryKey(Long specialID){
        return picSpecialMapper.selectByPrimaryKey(specialID);
    }


    /**
     * 根据图片ID查询专题
     * @param picId
     * @return
     */
    public Object selectListByPictureId(Long picId,Integer pageNum, Integer pageSize){

        PageHelper.startPage(pageNum, pageSize);//分页初始化
        Page page = (Page) picSpecialMapper.selectListByPictureId(picId);

        return new PageInfo(page, page.getPages(), page.getTotal());

    }


    /**
     * 根据用户ID查询专题
     * @param userId
     * @return
     */
    public Object selectListByUserId(Long userId,Integer pageNum, Integer pageSize){

        PageHelper.startPage(pageNum, pageSize);//分页初始化
        Page page = (Page) picSpecialMapper.selectListByUserId(userId);

        return new PageInfo(page, page.getPages(), page.getTotal());

    }

    /**
     * 根据专题ID查询图片列表
     * @param specialID
     * @return
     */
    public Object selectListPicBySpecialId(Long specialID,Integer pageNum, Integer pageSize){

        String orderBy = "addTime";
        String orderType = "desc";
        String orderStr = "special." + orderBy + " " + orderType;
        PageHelper.startPage(pageNum, pageSize,orderStr);//分页初始化
        Page page = (Page) picSpecialMapper.selectListPicBySpecialId(specialID);

        return new PageInfo(page, page.getPages(), page.getTotal());

    }

    /**
     * 根据专题ID删除当下所属图片
     * @param specialid
     * @return
     */
    public Object deletePicBySpecialId(Long specialid){
        return picSpecialDetailMapper.deleteBySpecialId(specialid);

    }

    /**
     * 添加专题图片
     * @param picSpecialVo
     * @return
     */
    public int addPicSpecialDetail(PicSpecialDetailVo picSpecialVo){
        try {
            int result=1;
            if (picSpecialVo.getPicturte().size() > 0) {
                for (String pic : picSpecialVo.getPicturte()) {
                    int count=picSpecialDetailMapper.selectByPicSpecialId(new QueryDetailDto(picSpecialVo.getSpecialid(),Long.parseLong(pic)));
                    if (count>0){
                        continue;
                    }
                    PicSpecialDetail detail = new PicSpecialDetail();
                    detail.setAddtime(CommonUtils.getCurTimestamp());
                    detail.setSpecialdetailid(IdGen.get().nextId());
                    detail.setPictureid(Long.parseLong(pic));
                    detail.setSpecialid(picSpecialVo.getSpecialid());
                    detail.setSpecialname(picSpecialVo.getSpecialname());
                    picSpecialDetailMapper.insertSelective(detail);
                }
            }
            return result;
        }catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.info(e.getMessage());
            throw CommonTools.createException(ErrorCodeEnum.operateOtherExceptionFailure);
        }
    }


    /**
     * 批量删除专题所属图片
     * @param detailIds
     */
    public Object delPicSpecialDetail(List<String> detailIds){
        try {
            int result=0;
             if (detailIds.size()>0){
                 for (String id:detailIds){
                     if (id!=null && !id.equals("")){
                         picSpecialDetailMapper.deleteByPrimaryKey(Long.parseLong(id));
                     }
                 }
             }
             return result;
        }catch (ServiceException se) {
            throw se;
        } catch (Exception e) {
            log.info(e.getMessage());
            throw CommonTools.createException(ErrorCodeEnum.operateOtherExceptionFailure);
        }
    }

}
